package com.project.daojishi;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class AddActivity extends AppCompatActivity {
    @BindView(R.id.delete)
    ImageView delete;
    @BindView(R.id.add)
    ImageView add;
    @BindView(R.id.et_title)
    EditText etTitle;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.sp_type)
    Spinner spType;
    @BindView(R.id.sp_chongfu)
    Spinner spChongfu;

    String type = "事件", chongfu = "重复";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.delete, R.id.add, R.id.tv_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.delete:
                finish();
                break;
            case R.id.add:
                Diary diary = new Diary();
                diary.setChongfu(chongfu);
                diary.setTime(tvTime.getText().toString());
                diary.setTitle(etTitle.getText().toString());
                diary.setType(type);
                SqliteDBUtils.getInstance(getApplicationContext()).save(diary);
                Toast.makeText(getApplicationContext(),"添加成功",Toast.LENGTH_SHORT).show();
                finish();
                break;
            case R.id.tv_time:
                TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        tvTime.setText(getTime(date));
                    }
                })
                        .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                            @Override
                            public void onTimeSelectChanged(Date date) {

                            }
                        })
                        .setType(new boolean[]{true, true, true, false, false, false})
                        .setItemVisibleCount(6)
                        .setLineSpacingMultiplier(2.0f)
                        .isAlphaGradient(true)
                        .build();
                pvTime.show();
                break;
        }
    }

    private String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);

    }
}
